# -*- coding: utf-8 -*-

import tkFileDialog
import tkMessageBox
import sys
import xmlrpclib

from xTkinter import *

class VueFacture(Frame):
    def __init__(self,master,parent):
        Frame.__init__(self,master)
        
        self.initServeur()
        self.parent =parent
        f=Frame(master)
        self.initServeur()
        self.listId=[]
        
        self.combo=xCombobox(self,width=20)
        self.combo.setLocation(10,10)
        self.combo.setListWidth(20)
        self.combo.setListHeight(5)

        self.combo.setValidator(self.choix)
        

        self.configure(width=550,height=700)
        
        requet="select * from s_customer"
        rep=self.serveur.ExecuterRequeteSQLWhere(requet)
        
        if(rep[0]==1):
            list =rep[1]
            for i in list:              
                self.combo.addItem(str(i[0]))
        else:
            print "reponse",self.rep[1]
            
        self.combo.setText("Id Client")
            

##============================================================
        text2=Label(self,text="Numéro")
        text2.place(x=10,y=55 )
        self.entry2 = Entry(self,width=10,bg='#F7F2B2')
        self.entry2.place(x=120,y=55)
        
#        self.Bouton=Button(self,text="Afficher Détails du Client",width=20)
#        self.Bouton.place(x=220,y=50)
        
        text3=Label(self,text="Nom").place(x=10,y=80 )
        self.entry3 = Entry(self,width=30,bg='#F7F2B2')
        self.entry3.place(x=120,y=80)
        
        text4=Label(self,text="Téléphone").place(x=10,y=100 )
        self.entry4 = Entry(self,width=20,bg='#F7F2B2')
        self.entry4.place(x=120,y=100)
        
        text5=Label(self,text="Adresse").place(x=10,y=120 )
        self.entry5 = Entry(self,width=60,bg='#F7F2B2')
        self.entry5.place(x=120,y=120)
        
        text6=Label(self,text="Ville").place(x=10,y=140 )
        self.entry6 = Entry(self,width=25,bg='#F7F2B2')
        self.entry6.place(x=120,y=140)
        
        text7=Label(self,text="Province").place(x=280,y=140 )
        self.entry7 = Entry(self,width=20,bg='#F7F2B2')
        self.entry7.place(x=340,y=140)
        
        text8=Label(self,text="Pays").place(x=10,y=160 )
        self.entry8 = Entry(self,width=20,bg='#F7F2B2')
        self.entry8.place(x=120,y=160)
        
        text9=Label(self,text="Code Postal").place(x=264,y=160 )
        self.entry9 = Entry(self,width=15,bg='#F7F2B2')
        self.entry9.place(x=340,y=160)
        
        text10=Label(self,text="Crédit").place(x=10,y=180 )
        self.entry10 = Entry(self,width=15,bg='#F7F2B2')
        self.entry10.place(x=120,y=180)
        
        text11=Label(self,text="ID du Représentant").place(x=10,y=200 )
        self.entry11 = Entry(self,width=10,bg='#F7F2B2')
        self.entry11.place(x=120,y=200)
        
        text11=Label(self,text="ID de la Région").place(x=10,y=220 )
        self.entry11 = Entry(self,width=10,bg='#F7F2B2')
        self.entry11.place(x=120,y=220)
        
#        text12=Label(self,text="Commentaires").place(x=10,y=280 )
#        self.entry12 = Text(self,width=60,bg='#F7F2B2')
#        self.entry12.place(x=120,y=280)

        
        
        
        
        self.place(x=0,y=0)
##================================================================

    def choix(self):
        self.entry2.delete(0,END)
        self.entry3.delete(0,END)
        self.entry4.delete(0,END)
        self.entry5.delete(0,END)
        self.entry6.delete(0,END)
        self.entry7.delete(0,END)
        self.entry8.delete(0,END)
        self.entry9.delete(0,END)
        self.entry10.delete(0,END)

        x=self.combo.getText()
        self.entry2.insert(0,x)
        requet1="select * from s_customer where id="+x
        rep1=self.serveur.ExecuterRequeteSQLWhere(requet1)
        
        if(rep1[0]==1):
            list1 =rep1[1]
            if list1[0][1]:
                self.entry3.insert(0,list1[0][1])
            if list1[0][2]:
                self.entry4.insert(0,list1[0][2])
            if list1[0][3]:
                self.entry5.insert(0,list1[0][3])
            if list1[0][4]:
                self.entry6.insert(0,list1[0][4])
            if list1[0][5]:
                self.entry7.insert(0,list1[0][5])
            if list1[0][6]:
                self.entry8.insert(0,list1[0][6])
            if list1[0][7]:
                self.entry9.insert(0,list1[0][7])
            if list1[0][8]:
                self.entry10.insert(0,list1[0][8])
            if list1[0][9]:
                self.entry11.insert(0,list1[0][9])
#            if list1[0][10]:
#                self.entry12.insert(0,self.getText(list1[0][10]))
            self.Bouton=Button(self,text="Creer la commande",width=20, command=self.creerFacture)
            self.Bouton.place(x=220,y=50)
 
        else:
            print "reponse",self.rep1[1]
 
    def initServeur(self):        
        PortServeur=':8000'
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        pass 
    
    def creerFacture(self):
        text11=Label(self,text="FACTURE").place(x=10,y=300 )
        self.combo1=xCombobox(self,width=20)
        self.combo1.setLocation(10,330)
        self.combo1.setListWidth(20)
        self.combo1.setListHeight(10)

        self.combo1.setValidator(self.choix1)
        requet2="select * from s_product"
        rep=self.serveur.ExecuterRequeteSQLWhere(requet2)
        
        if(rep[0]==1):
            list =rep[1]
            for i in list:              
                self.combo1.addItem(str(i[0]))
        else:
            print "reponse",self.rep[1]
        
        self.combo1.setText("Id Produit")
        
        text13=Label(self,text="ID du produit").place(x=10,y=360 )
        self.entry13 = Entry(self,width=13)
        self.entry13.place(x=10,y=380)
        text14=Label(self,text="Nom du produit").place(x=120,y=360 )
        self.entry14 = Entry(self,width=25)
        self.entry14.place(x=100,y=380)
        text15=Label(self,text="Decription").place(x=260,y=360 )
        self.entry15 = Entry(self,width=25)
        self.entry15.place(x=260,y=380)
        text16=Label(self,text="Prix").place(x=400,y=360 )
        text17=Label(self,text="Quntité").place(x=450,y=360 )
        text18=Label(self,text="Total").place(x=500,y=360 )
        
    
    def choix1(self):
        y=self.combo1.getText()
 #       self.entry2.insert(0,x)
        requet3="select * from s_product where id="+y
        rep1=self.serveur.ExecuterRequeteSQLWhere(requet3)
        
        pass


if __name__ == '__main__':
    root=Tk()
    root.geometry("600x700")
    root.title("PERP | Calcul Facture")
    Client=VueFacture(root,None)
    root.mainloop()